Imports System.Net
Imports System.IO
Imports System.Text


Public Class Daz3DDownloader
    Inherits BaseDownloader

    Private Const BASE_PAGE_URL As String = "http://www.daz3d.com/i.x/shop/itemdetails/-/?item={0}"
    Private Const IMAGE_URL As String = "http://www-cache.daz3d.com/store/item_file/{0}/image_medium.jpg"

    Private Const URL_TO_REMOVE As String = "http://www.daz3d.com/i.x/shop/itemdetails/-/?item="

    'Private Const PRODUCT_CODE_START_MATCH As String = "<b>Product Code:</b>&nbsp;&nbsp;"
    Private Const PRODUCT_CODE_START_MATCH As String = ">product code:&nbsp;&nbsp;"
    Private Const PRODUCT_CODE_END_MATCH As String = "("

    Private Const REQUIRED_PRODUCT_START_MATCH As String = "<span class=""requiredProduct"">Required Products</span>"
    Private Const REQUIRED_PRODUCT_END_MATCH As String = "</td>"
    Private Const REQUIRED_PRODUCT_SEPERATOR_START As String = "<A HREF="
    Private Const REQUIRED_PRODUCT_SEPERATOR_START_END As String = "=rq"">"
    Private Const REQUIRED_PRODUCT_SEPERATOR_END As String = "</A>"

    Private Const PRICE_START_MATCH As String = "<span class=""bodyGry11"">Reg. Price: $"
    Private Const PRICE_END_MATCH As String = "</span>"

    Private Const PRODUCT_NAME_START_MATCH As String = "<td class=""prodTitle20"" align=""left"" >"
    Private Const PRODUCT_NAME_END_MATCH As String = "<br />"

    Public Const REQUIRED_PRODUCT_SEPERATOR As String = ", "


    Private Const UPDATE_SQL As String = "UPDATE Files SET product_link = '{0}' WHERE code= '{1}' AND provider = 'Daz3D'"

    Public Const LINK_FILENAME As String = "DazLinks.xml"

    Public Overrides ReadOnly Property BasePageUrl() As String
        Get
            Return BASE_PAGE_URL
        End Get
    End Property

    Public Overrides ReadOnly Property BaseImageUrl() As String
        Get
            Return IMAGE_URL
        End Get
    End Property

    Public Overrides ReadOnly Property LinkFilename() As String
        Get
            Return LINK_FILENAME
        End Get
    End Property

    Public Overrides ReadOnly Property UrlToRemove() As String
        Get
            Return URL_TO_REMOVE
        End Get
    End Property

    Public Sub New(ByVal imageDirectory As String, _
                   ByVal imageHolder As PictureBox)
        MyBase.New(imageDirectory, _
                    imageHolder)
    End Sub
   

    Private Function GetProductName(ByVal pageContents As String) As String

        Dim productCodeStart As Integer = pageContents.IndexOf(PRODUCT_NAME_START_MATCH)
        Dim productName As String = ""

        If productCodeStart > -1 Then
            pageContents = pageContents.Substring(productCodeStart)
            pageContents = pageContents.Substring(0, pageContents.IndexOf(PRODUCT_NAME_END_MATCH))
            pageContents = pageContents.Replace(PRODUCT_NAME_START_MATCH, EMPTY)
            productName = pageContents.Trim
        End If

        Return productName

    End Function

    Private Function GetPrice(ByVal pageContents As String) As Single

        Dim productCodeStart As Integer = pageContents.IndexOf(PRICE_START_MATCH)
        Dim price As Single = 0.0

        If productCodeStart > -1 Then
            pageContents = pageContents.Substring(productCodeStart)
            pageContents = pageContents.Substring(0, pageContents.IndexOf(PRICE_END_MATCH))
            pageContents = pageContents.Replace(PRICE_START_MATCH, EMPTY)
            price = CType(pageContents, Single)
        End If

        Return price

    End Function

    Private Function GetRequiredProducts(ByVal pageContents As String) As String

        Dim requiredProducts As String = ""
        Dim requiredProductTemp As String = ""

        Dim requiredProductsStart As Integer = pageContents.IndexOf(REQUIRED_PRODUCT_START_MATCH)

        If requiredProductsStart > -1 Then
            pageContents = pageContents.Substring(requiredProductsStart)
            pageContents = pageContents.Substring(0, pageContents.IndexOf(REQUIRED_PRODUCT_END_MATCH))
            pageContents = pageContents.Replace(REQUIRED_PRODUCT_START_MATCH, EMPTY)
            requiredProductsStart = pageContents.IndexOf(REQUIRED_PRODUCT_SEPERATOR_START_END)
            While requiredProductsStart > -1
                requiredProductTemp = pageContents.Substring(requiredProductsStart)
                requiredProductTemp = requiredProductTemp.Substring(0, requiredProductTemp.IndexOf(REQUIRED_PRODUCT_SEPERATOR_END))
                requiredProductTemp = requiredProductTemp.Replace(REQUIRED_PRODUCT_SEPERATOR_START_END, EMPTY)
                If requiredProducts.Length > 0 Then
                    requiredProducts &= REQUIRED_PRODUCT_SEPERATOR
                End If
                requiredProducts &= requiredProductTemp
                pageContents = pageContents.Substring(pageContents.IndexOf(REQUIRED_PRODUCT_SEPERATOR_END) + REQUIRED_PRODUCT_SEPERATOR_END.Length)
                requiredProductsStart = pageContents.IndexOf(REQUIRED_PRODUCT_SEPERATOR_START_END)
            End While
        End If

        Return requiredProducts

    End Function

    Protected Overrides Sub DownLoadPage(ByVal itemId As Integer)

        Dim pageContents As String = GetPageContents(itemId)
        Dim requiredProducts As String = ""
        Dim productName As String = ""
        Dim price As Single = 0.0

        Dim productCodeStart As Integer = pageContents.IndexOf(PRODUCT_CODE_START_MATCH)

        If productCodeStart > -1 Then

            requiredProducts = GetRequiredProducts(pageContents)
            price = GetPrice(pageContents)
            productName = GetProductName(pageContents)

            pageContents = pageContents.Substring(productCodeStart)
            pageContents = pageContents.Substring(0, pageContents.IndexOf(PRODUCT_CODE_END_MATCH))
            pageContents = pageContents.Replace(PRODUCT_CODE_START_MATCH, EMPTY)

            _linkFile.AddLink(pageContents, _
                              String.Format(BASE_PAGE_URL, itemId.ToString), _
                              itemId, _
                              requiredProducts, _
                              productName, _
                              price)

        End If

    End Sub

    Public Overrides Sub Download()

        Try

            Dim MinDazId As Integer = _linkFile.MaxId + 1

            Download(MinDazId, _
                     My.Settings.MaxDaz3DItemId)

        Catch ex As Exception

        End Try
        _linkFile.WriteFile(LINK_FILENAME)

    End Sub

End Class
